﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class TaskDialogAddMovieStep3 : Form
	{
		private List<Movie> movies = new List<Movie>();

		private string status = "Adding movie {0} of {1}";

		private MovieBL mbl = new MovieBL();

		public List<Movie> Movies
		{
			get { return movies; }
			set { movies = value; }
		}

		public TaskDialogAddMovieStep3()
		{
			InitializeComponent();
		}

		public TaskDialogAddMovieStep3(List<Movie> movies)
		{
			this.movies = movies;

			InitializeComponent();
		}

		private void TaskDialogAddMovieStep3_Load(object sender, EventArgs e)
		{
			progressBarAdding.Maximum = movies.Count;
			labelStatus.Text = String.Format(status, 0, movies.Count);
			backgroundWorkerAddIssues.RunWorkerAsync(movies);
		}

		private void backgroundWorkerAddIssues_DoWork(object sender, DoWorkEventArgs e)
		{
			for (int i = 0; i < movies.Count; i++)
			{
				if (backgroundWorkerAddIssues.CancellationPending)
				{
					e.Cancel = true;
					return;
				}

				if (movies[i].Poster == "http://hpneo.com/DoubleFeature/img/poster_default.jpg")
					movies[i] = mbl.Create(movies[i]);

				if (mbl.Save(movies[i]))
				{
					//adding counter for update objectlistview
				}

				backgroundWorkerAddIssues.ReportProgress(i, movies[i]);
				System.Threading.Thread.Sleep(500);
			}
		}

		private void backgroundWorkerAddIssues_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			progressBarAdding.Value = e.ProgressPercentage + 1;
			labelStatus.Text = String.Format(status, progressBarAdding.Value, movies.Count);
		}

		private void backgroundWorkerAddIssues_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if (movies.Count == 1)
				MessageBox.Show("Your movie was added", "Operation completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
			else
				MessageBox.Show("Your movies were added", "Operation completed", MessageBoxButtons.OK, MessageBoxIcon.Information);

			DialogResult = DialogResult.OK;
			Close();
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			backgroundWorkerAddIssues.CancelAsync();
			DialogResult = DialogResult.Cancel;
		}
	}
}
